<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="配送单位名称" prop="logisticsName">
        <el-input
          v-model="queryParams.logisticsName"
          placeholder="请输入配送单位名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['basic:logistics:add']"
        >新增
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading"  border :data="logisticsList">
      <el-table-column label="编号" align="center" prop="id" width="60"/>
      <el-table-column label="配送单位名称" align="center" prop="logisticsName" :show-overflow-tooltip="true"/>
      <el-table-column label="配送单位ID" align="center" prop="logisticsId" :show-overflow-tooltip="true"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" width="100px" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['basic:logistics:delete']"
          >作废
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 新增对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" @close="reset" append-to-body>
      <el-form :model="form" ref="form"  :rules="rules" label-width="100px" class="demo-ruleForm">
        <el-row>
          <el-form-item label="配送单位" prop="customerId">
            <el-select v-model="form.customerId" filterable @change="setData">
              <el-option v-for="item in customerOptions" :key="item.value" :label="item.label" :value="item.value"/>
            </el-select>
          </el-form-item>
        </el-row>
        <el-form-item label="配送单位ID" prop="logisticsId">
          <el-input v-model="form.logisticsId" readonly/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import {getLogistics, addLogistics, deleteLogistics, listMsfxCustomer} from '@/api/msfx/logistics.js'


export default {
  name: "Logistics",
  dicts: ['basic_info_factory'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 是否多选
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 配送单位列表
      logisticsList: [],
      // 工厂信息
      factoryOptions: [],
      //码上放心往来单位信息
      customerOptions: [],
      //码上放心往来单位列表
      customerList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        logisticsName: ''
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        customerId: [{
          required: true, message: '请选择配送单位', trigger: 'blur'
        }]
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询配送单位列表 */
    getList() {
      this.loading = true;
      getLogistics(this.queryParams).then(response => {
          this.logisticsList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    // 表单重置
    reset() {
      this.form = {};
      this.customerOptions = [];
      this.customerList = [];
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    handleAdd() {
      this.getMsfxCustomerInfo()
      this.open = true;
      this.title = "新增配送单位";
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    getMsfxCustomerInfo() {
      this.customerOptions = [];
      this.customerList = [];
      listMsfxCustomer().then(response => {
        this.customerList = response.data;
        this.customerList.forEach(item => {
          this.customerOptions.push({
            label: item.customerName,
            value: item.customerId
          })
        })
      })
    },

    // 根据下拉框选择内容显示数据
    setData() {
      this.customerList.forEach(item => {
        if (item.customerId === this.form.customerId) {
          this.form.logisticsName = item.customerName;
          this.form.logisticsId = item.customerId;
        }
      })
    },

    // 表格提交处理
    submitForm() {
      this.$refs["form"].validate(valid => {
          if (valid) {
            addLogistics(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      )
    },

    //删除
    handleDelete(row){
      this.$modal.confirm('是否确认作废配送单位编号为"' + row.id + '"的数据项？').then(function () {
        return deleteLogistics(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("作废成功");
      }).catch(() => {
      });
    }
  }
};
</script>
